// 为了兼容性存在的  fetch是基于promise，但是ie的低端浏览器能支持promise?
import 'whatwg-fetch'
import 'es6-promise'

// 将对象拼接成 key1=val1&key2=val2&key3=val3 的字符串形式
function params(obj) {
	var result = "";
	var item;
	for (item in obj) {
		result += "&" + item + "=" + encodeURIComponent(obj[item]);
	}
	if (result) {
		result = result.slice(1);
	}
	return result;
}

export function getHttp(url,paramsObj){
	if(paramsObj){
		console.log(url+"?"+params(paramsObj));
		return fetch(url+"?"+params(paramsObj),{
			credentials: 'include',
			headers: {
				'Accept': 'application/json, text/plain, */*'
			}
		}).then(r=>r.json()).catch(e=>console.error("请求超时..."))
	}else{
		return fetch(url,{
			credentials: 'include',
			headers: {
				'Accept': 'application/json, text/plain, */*'
			}
		}).then(r=>r.json()).catch(e=>console.error("请求超时..."))
	}
}